//
//  TransitionViewController.m
//  基本框架
//
//  Created by runone02 on 2018/7/25.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "TransitionViewController.h"
#import "TransitionAnimation.h"
#import "CardView.h"
@interface TransitionViewController ()<UIViewControllerTransitioningDelegate>

/** */
@property (nonatomic, strong) NSMutableArray *cards;
/** */
@property (nonatomic, strong) NSMutableArray *showCards;
/** */
@property (nonatomic, assign) NSUInteger currentIndex;
/** */
@property (nonatomic, assign) NSInteger showCardsNumber;
/** */
@property (nonatomic, assign) CGPoint initialLocation;
/** */
@property (nonatomic, strong) NSMutableArray *imageArray;
/** */
@property (nonatomic, strong) NSMutableArray *alphaArray;

@end

@implementation TransitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.showCardsNumber = 4;
    [self addCardView];
    [self setupNavView];
}


- (void) setupNavView{
    [super setupNavView];
    WEAKSELF;
    self.navView.backgroundColor = [UIColor clearColor];
    self.navView.showBottomLabel = NO;
    [self.navView.leftButton setImage:backArrowWhiteIcon forState:UIControlStateNormal];

    self.navView.leftButtonBlock = ^{
        
        [weakSelf.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    };
}
- (instancetype)init{
    if (self = [super init]) {
        self.transitioningDelegate = self;
    }
    return self;
}

- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
        for (int i = 0; i < IMAGE_NUMBER; i++) {
            [_imageArray addObject:[NSString stringWithFormat:@"image%d.jpg",i]];
        }
    }
    return _imageArray;
}

- (NSMutableArray *)alphaArray{
    if (!_alphaArray) {
        _alphaArray = [[NSMutableArray alloc]initWithArray:@[@(0),@(0.35),@(0.70),@(1.0)]];
    }
    return _alphaArray;
}

- (NSMutableArray *)cards{
    if (!_cards) {
        _cards = [[NSMutableArray alloc]init];
        
        for (int i = 0; i < IMAGE_NUMBER; i++) {
            CardView *carView = [[CardView  alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
            carView.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0 + OFFSET_Y *(IMAGE_NUMBER - 1 - i));carView.transform = CGAffineTransformMakeScale(1- sizePercent *(IMAGE_NUMBER - i), 1 - sizePercent *(IMAGE_NUMBER - i));
            carView.layer.cornerRadius = 10;
            carView.layer.masksToBounds = YES;
            carView.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:self.imageArray[i]].CGImage);
            [_cards addObject:carView];
        }
    }
    return _cards;
}


- (NSMutableArray *)showCards{
    if (!_showCards) {
        _showCards = [[NSMutableArray alloc]init];
        
    }
    return _showCards;
}

- (void)addCardView{
    if (IMAGE_NUMBER < _showCardsNumber) {
        NSInteger removeNum = _showCardsNumber - IMAGE_NUMBER;
        _showCardsNumber = IMAGE_NUMBER;
        for (int i = 0; i <removeNum; i++) {
            [self.alphaArray removeObjectAtIndex:0];
        }
        for (int i = 0; i < _showCardsNumber; i++) {
            CardView *cardView = self.cards[i];
            cardView.alpha = [self.alphaArray[i]floatValue];
            [self.view addSubview:cardView];
            if (i == _showCardsNumber - 1) {
                [self addPanGestureWithView:cardView];
                self.currentIndex = _showCardsNumber - 1;
            }
        }
    }else{
        for (int i = 0; i < _showCardsNumber; i++) {
            CardView *cardView = self.cards[IMAGE_NUMBER - _showCardsNumber + i];
            cardView.alpha = [self.alphaArray[i]floatValue];
            [self.view addSubview:cardView];
            if (i == _showCardsNumber - 1) {
                [self addPanGestureWithView:cardView];
                self.currentIndex = IMAGE_NUMBER - 1;
            }
        }
    }
}

/** 添加手势 */
- (void)addPanGestureWithView:(CardView *)carView{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panHan:)];
    [carView addGestureRecognizer:pan];
}

- (void)panHan:(UIPanGestureRecognizer *)pan{
    CardView *cardview = self.cards[self.currentIndex];
    
    //开始拖动
    if (pan.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始拖动");
    }
    else{ //拖动中
        //给顶部视图添加动画
        CGPoint transLcation = [pan translationInView:cardview];
        cardview.center = CGPointMake(cardview.center.x + transLcation.x, cardview.center.y + transLcation.y);
        
        CGFloat XOffPercenr = (cardview.center.x - SCREEN_WIDTH/2.0)/(SCREEN_HEIGHT / 2.0);
        
        CGFloat rotation = M_PI_2/4 *XOffPercenr;
        
        cardview.transform = CGAffineTransformMakeRotation(rotation);
        [pan setTranslation:CGPointZero inView:cardview];
        
        //给其余底部视图添加动画
        [self animationBlowViewWithXOffPercent:fabs(XOffPercenr)];
    }
    //拖动结束
    if (pan.state == UIGestureRecognizerStateEnded) {
        //视图不移除
        if (cardview.center.x > DISTANCE_X && cardview.center.x < SCREEN_WIDTH - DISTANCE_X) {
            [UIView animateWithDuration:0.23 animations:^{
                cardview.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/ 2.0);
                cardview.transform = CGAffineTransformMakeRotation(0);
                [self animationBlowViewWithXOffPercent:0];
            }];
        }
        //移除拖动视图
        else{
            //视图在屏幕左侧移除
            if (cardview.center.x < DISTANCE_X) {
                [UIView animateWithDuration:0.25 animations:^{
                    cardview.center = CGPointMake(0 - 200, cardview.center.y);
                }];
            }
            //视图在屏幕右侧移除
        else{
                [UIView animateWithDuration:0.25 animations:^{
                    cardview.center = CGPointMake(SCREEN_WIDTH + 200, cardview.center.y);
                }];
            }
            [self animationBlowViewWithXOffPercent:1];
            [self performSelector:@selector(cardRemove:) withObject:cardview afterDelay:0.25];
            
        }
    }
}

- (void)animationBlowViewWithXOffPercent:(CGFloat)XOffPercent {
    for (int i = 0; i < _showCardsNumber - 1; i++) {
        CardView * otherView = self.cards[self.currentIndex-i-1];
        //透明度
        CGFloat alpha = [self.alphaArray[_showCardsNumber-i-2] floatValue] + ([self.alphaArray[_showCardsNumber-i-1] floatValue] - [self.alphaArray[_showCardsNumber-i-2] floatValue])*XOffPercent;
        otherView.alpha = alpha;
        //中心
        CGPoint point = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0 + OFFSET_Y*(i+1) - OFFSET_Y*XOffPercent);
        otherView.center = point;
        //缩放大小
        CGFloat scale = 1-sizePercent*(i+1)+ XOffPercent*sizePercent;
        otherView.transform = CGAffineTransformMakeScale(scale, scale);
    }
}

- (void)cardRemove:(CardView *)card{
    if (card) {
        [card removeFromSuperview];
    }
    self.currentIndex--;
    if ((NSInteger)self.currentIndex < 0) {
        return;
    }
    [self addPanGestureWithView:self.cards[self.currentIndex]];
    if ((NSInteger)(self.currentIndex-_showCardsNumber+1) >= 0) {
        //添加一个视图
        CardView *cardView = self.cards[self.currentIndex-_showCardsNumber+1];
        cardView.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0 + OFFSET_Y*(_showCardsNumber-1));
        cardView.transform = CGAffineTransformMakeScale(1-sizePercent*(_showCardsNumber-1), 1-sizePercent*(_showCardsNumber-1));
        cardView.alpha = 0;
        [self.view addSubview:cardView];
        [self.view insertSubview:cardView belowSubview:self.cards[self.currentIndex-_showCardsNumber+2]];
    }else{
        if (IMAGE_NUMBER < _showCardsNumber) {
            _showCardsNumber--;
            [self.alphaArray removeObjectAtIndex:0];
        }else{
            _showCardsNumber--;
            if (_showCardsNumber < 4) {
                [self.alphaArray removeObjectAtIndex:0];
            }
        }
        
    }
}

#pragma mark -- UIViewControllerTransitioningDelegate

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    
    return [TransitionAnimation transitionWhithTransitionType:TransitionTypePresent];
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [TransitionAnimation transitionWhithTransitionType:TransitionTypeDismiss];
}
@end
